package com.xltt.newbleclient;

import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;

import com.xltt.byteutil.NotificationMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Logger;

public class NotificationMonitorService extends NotificationListenerService {

    /**
     * 是否手表监听应用
     * @param pkg
     * @return
     */
    private boolean isWatchApp(String pkg){
        if(pkg.equals("com.tencent.mm") || pkg.equals("com.android.dialer")
                || pkg.equals("com.android.mms") || pkg.equals("com.whatsapp")){
            return true;
        }
        return false;
    }

    // 在收到消息时触发
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        if(!isWatchApp(notificationPkg)){
            //Log.d("zhanghui","过滤不是手表监听数据的应用 = " + notificationPkg);
            return;
        }
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息的抬头
        String notificationTitle = (String) extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        CharSequence notificationContentData = extras.getCharSequence(Notification.EXTRA_TEXT);
        if(notificationContentData == null){
            return;
        }
        String notificationContent = notificationContentData.toString();
        //String notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT).toString();
//        Log.i("zhanghui", "Notification posted " + notificationTitle + " & " + notificationText
//                + " & " + notificationSubText);
        Log.d("zhanghui", "包名 = " + notificationPkg + "， 标题 =  " + notificationTitle
                + " ，内容 = " + notificationContent);
        if(TextUtils.isEmpty(notificationPkg) || TextUtils.isEmpty(notificationTitle) || TextUtils.isEmpty(notificationContent)){
            return;
        }
        NotificationMessage msg = new NotificationMessage();
        msg.setPackageName(notificationPkg);
        //msg.setTitle(notificationTitle);
        msg.setContent(formatNoticationsContent(notificationPkg,notificationTitle,notificationContent));
        msg.setPost(true);//用以区分是post还是removed
        Log.d("zhanghui",msg.toString());
        EventBus.getDefault().post(msg);//发消息给MainActivity
    }

    // 在删除消息时触发
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        /*
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = (String) extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        //String notificationText = (String) extras.getString(Notification.EXTRA_TEXT);
        CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
        CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT);
        Log.i("zhanghui", "Notification removed " + notificationTitle + " & " + notificationText
                + " & " + notificationSubText);
        NotificationMessage msg = new NotificationMessage();
        msg.setPackageName(notificationPkg);
        msg.setTitle(notificationTitle);
        msg.setContent(notificationText + "");
        msg.setPost(false);//用以区分是post还是removed
        if (notificationTitle != null || notificationText != null) {
            EventBus.getDefault().post(msg);//发消息给MainActivity
        }
        */
    }

    /**
     * @param pkg
     * @param title
     * @param content
     * @return
     */
    private String mWhatsAppTitle,mWhatsAppContent;
    private String formatNoticationsContent(String pkg,String title,String content){
        if(pkg.equals("com.tencent.mm")){//微信内容优化
            int subIndex = content.indexOf("]");//截取消息[5条]Zzzzzzz: 请问
            if(subIndex != -1){
                content = content.substring(subIndex + 1);
            }
            return content;
        }else if(pkg.equals("com.android.mms")){//用户手机短信
            if(title.contains("条新信息") || title.contains("new messages")){//中英文通知
                if(content.indexOf(",") != -1){
                    content = content.substring(0,content.indexOf(","));
                }else if(content.indexOf("、") != -1){
                    content = content.substring(0,content.indexOf("、"));
                }
                return "New message，form:" + content;
            }else{//单条新未读消息
                return "New message，form:" + title + " content：" + content;
            }
        }else if(pkg.equals("com.android.dialer") || pkg.equals("com.android.incallui")){//用户手机来电
            return "New call， content：" + content;
        }else if(pkg.equals("com.whatsapp")){//马来聊天工具wahtsApp
            if(content.contains("条新消息") || content.contains("new messages")){//单人消息
                return mWhatsAppTitle + ":" + mWhatsAppContent;
            }
            mWhatsAppTitle = title;
            mWhatsAppContent = content;
            return title + ":" + content;
        }
        return content;
    }


}
